﻿using System.Collections.Generic;
using PushBox.Core;

namespace PushBox.Console
{
    internal class Program
    {
        static Map map;

        static Queue<string> mapInfos = new Queue<string>();

        static void Main(string[] args)
        {
            string mapDraw = @"
 ########
##  +  +#
#   *#  #
#  * #  ##
#  @     #
##########
";
            mapInfos.Enqueue(mapDraw);
            mapDraw = @"
 ###########
##  +     +#
#   *#     #
#  * #    ##
#        ##
#  @     #
#        #
##########
";
            mapInfos.Enqueue(mapDraw);
            mapDraw = @"
 ##########
##  +     #
#   *#    #
#  @     #
#        #
##########
";
            mapInfos.Enqueue(mapDraw);



            map = new Map(mapDraw, ShowMap);
            map.Passed += Map_Passed;   //通关事件
            while (true)
            {
                System.ConsoleKeyInfo key = System.Console.ReadKey();
                switch (key.Key)
                {
                    case System.ConsoleKey.UpArrow:
                        map.Gamer.TryMove(MoveType.Up);
                        break;
                    case System.ConsoleKey.DownArrow:
                        map.Gamer.TryMove(MoveType.Down);
                        break;
                    case System.ConsoleKey.LeftArrow:
                        map.Gamer.TryMove(MoveType.Left);
                        break;
                    case System.ConsoleKey.RightArrow:
                        map.Gamer.TryMove(MoveType.Right);
                        break;
                }
            }
        }

        private static void Map_Passed(Map sender)
        {
            System.Console.WriteLine("通关了，回车继续");
            System.Console.ReadLine();
            System.Console.Clear();
            string m = mapInfos.Dequeue();
            map = new Map(m, ShowMap);
            map.Passed += Map_Passed;
        }

        static void ShowMap(Map m)
        {
            System.Console.CursorVisible = false;
            System.Console.SetCursorPosition(0, 0);
            for (int y = 0; y < m.Height; y++)
            {
                for (int x = 0; x < m.Width; x++)
                {
                    Position p = m.GetObject(x, y);
                    if (p == null) System.Console.Write(" ");
                    else if (p is Wall) System.Console.Write("#");
                    else if (p is Player) System.Console.Write("@");
                    else if (p is Target) System.Console.Write("+");
                    else if (p is Box) System.Console.Write("*");
                }
                System.Console.WriteLine();
            }
        }
    }
}
